package com.fulu.netty.luban.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * HeapByteBuffer 堆内，底层byte数组
 * DirectByteBuffer 堆外，对象在jvm里面，成员变量address直接引用堆外内存
 * DirectByteBuffer 性能更好
 * netty基于DirectByteBuffer
 */
public class Demo8 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("demo3.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("demo8.txt");

        FileChannel inputChannel = fileInputStream.getChannel();
        FileChannel outChannel = fileOutputStream.getChannel();

        while (true){
            // allocateDirect开一个堆外缓存
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(100);
            int read = inputChannel.read(byteBuffer);
            System.out.println(read);
            if(read == -1){
                break;
            }
            byteBuffer.flip();
            outChannel.write(byteBuffer);
        }
        fileOutputStream.close();
    }
}
